package cn.com.api.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_member_partner;
import cn.com.api.entity.ApiT_member_wemedia;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_org;
import cn.com.api.entity.U_article;
import cn.com.api.entity.U_cate;
import cn.com.api.mapper.ApiShopsMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiSmslogService;
import cn.com.api.service.ApiT_cityService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.api.service.ApiT_member_wemediaService;
import cn.com.api.service.ApiU_orgService;
import cn.com.api.service.ApiU_user_labelService;
import cn.com.api.service.CheckService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.service.U_articleService;
import cn.com.api.service.U_cateService;
import cn.com.util.*;

/**
 * 
 * ClassName: ApiMemberPartnerController
 * 
 * @Description: 自媒体模块接口
 * @author yewl
 * @date 2016年12月29日
 */
@Controller
@RequestMapping(Utils.SYS_API + "wemedia/")
public class ApiMemberWemediaController {

	@Autowired
	private U_articleService u_articleService;
	@Autowired
	private ApiMemberService apiMemberService;
	@Autowired
	private ApiT_memberService memberService;
	@Autowired
	private ApiU_orgService apiU_orgService;
	@Autowired
	private ApiT_member_wemediaService apiT_member_wemediaService;
	@Autowired
	private U_cateService U_cateService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiMemberWemediaController.class);

	/**
	 * 申请成为自媒体
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param partner
	 *            接收申请合伙人信息资料的实体类，接口参数名为实体属性名即可
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "applyWemedia")
	public void applyWemedia(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiT_member_wemedia wemedia) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				wemedia.setStatus(0);
				wemedia.setApply_time(DateUtil.nowTimeToString());
				paramMap.clear();
				paramMap.put("user_id", user_id);
				List<ApiT_member_wemedia> list = apiT_member_wemediaService.findT_member_wemediasByMap(paramMap);
				if (list != null && list.size() > 0) {
					wemedia.setId(list.get(0).getId());
					apiT_member_wemediaService.updateT_member_wemedia(wemedia);
				} else {
					apiT_member_wemediaService.insertT_member_wemedia(wemedia);
				}

				paramMap.clear();
				paramMap.put("cid", user_id);
				paramMap.put("verify_media", "2");
				this.apiMemberService.modifyApiTmemberByCuserid(paramMap);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, wemedia);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取申请成为自媒体的申请资料
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getWemedia")
	public void getWemedia(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map.put("user_id", user_id);
				List<ApiT_member_wemedia> list = apiT_member_wemediaService.findT_member_wemediasByMap(map);
				map.clear();
				if (list != null && list.size() > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功");
					map.put(Utils.DATAS, list.get(0));
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "无申请记录");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
